﻿using System;
using System.Collections;
using System.Collections.Generic;
using Data;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    void Awake()
    {
        AccountHandle.OtherLoginHandler += OtherLogin;
    }
    void OnDestroy()
    {
        AccountHandle.OtherLoginHandler -= OtherLogin;
    }
    // Use this for initialization
    void Start()
    {
        //初始化自己的坦克
      var local=  CreateTank(ActorManager.Instance.localUser);
        local.gameObject.AddComponent<LocalPlayer>();
        //初始化相机跟随
        TargetFollow.Instance.Init(local.transform);
        ActorManager.Instance.localTank = local;
        //初始化别的坦克
        if (ActorManager.Instance.otherUsers != null)
        {
            foreach (var item in ActorManager.Instance.otherUsers)
            {
                CreateTank(item);
            }
        }
    }
    //创建坦克工厂
    public Tank CreateTank(Account acc)
    {
        GameObject prefab = Resources.Load<GameObject>("Tank");
        var go = GameObject.Instantiate(prefab);
        Tank tank = go.GetComponent<Tank>();
            tank.Init(acc);

        ActorManager.Instance.AddTank(acc.username, tank);
        return tank;
    }
    //创建新登陆的tank
    private void OtherLogin(Account user)
    {
        CreateTank(user);
    }
}
